package dao

import (
	"SimpleDockerPanel/pkg/utils"
)

type User struct {
	Id       int
	Name     string
	Password string
	Email    string
	GroupId  int
}

func (User) TableName() string {
	return "user"
}

// GetUserByName 根据用户名获取用户信息
func GetUserByName(username string) (User, error) {
	var user User
	err := Db.Where("name = ?", username).First(&user).Error
	return user, err
}

// GetUserByEmail 根据邮箱获取用户信息
func GetUserByEmail(email string) (User, error) {
	var user User
	err := Db.Where("email = ?", email).First(&user).Error
	return user, err
}

func GetUserById(id int) (User, error) {
	var user User
	err := Db.Where("id = ?", id).First(&user).Error
	return user, err
}
func InsertUser(username, password, email string) (User, error) {
	user := User{
		Name:     username,
		Password: utils.EncryMd5(password),
		Email:    email,
		GroupId:  DefaultGroupId,
	}
	err := Db.Save(&user).Error
	return user, err
}
